<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" @row-click="handleRowClick" :height="autoHeight">
      <el-table-column prop="pictureName" label="签章" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <div><el-tag>{{scope.row.pictureName}}</el-tag></div>
          <el-image
              style="width: 100px; height: 100px"
              :src="signUrl(scope.row)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="70px">
        <template slot="header">
          <el-button v-if="!select" type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
        </template>
        <div slot-scope="scope">
          <template v-if="select">
            <el-button type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
          </template>
          <template v-else>
            <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
          </template>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="签章上传" width="400px">
      <el-upload
          :action="uploadUrl"
          :headers="headers"
          :on-success="uploadSuccess"
          :show-file-list="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "SignPicture",
  mixins: [IdeaTable],
  props: {
    select: {
      type: Boolean,
      default: false
    }
  },
  computed:{
    autoHeight() {
      return `calc(100vh - 130px)`
    },
    uploadUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API,  '/signPicture/upload')
    },
    headers () {
      return { Authorization: 'Bearer ' + this.$AuthStorage.getAccessToken() }
    }
  },
  created() {
    this.queryUrl = '/signPicture/queryPage'
    this.saveUrl = '/signPicture/save'
    this.removeUrl = '/signPicture/delete/'
    this.rowKey = 'pictureId'
    this.pageInfo.sorts.push({field: 'updateAt', asc: false })
    this.pageInfo.pageSize = 10
    this.loadSource()
  },
  methods: {
    signUrl(row){
      // preview
      const url = this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/signPicture/preview/' + row.pictureId)
      return url + '?access_token=' + this.$AuthStorage.getAccessToken()
    },
    uploadSuccess (response) {
      this.$resultMsg(this, response)
      if (response.code === 200) {
        this.loadSource()
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>

</style>
